package com.ranga.mselfcare;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ranga.mselfcare.entity.Transaction;

public class TransactionListAdapter extends BaseAdapter {

	private ArrayList<Transaction> transactionList;
	private Activity activity;
	private LayoutInflater inflater = null;
	
	public TransactionListAdapter(ArrayList<Transaction> tList, Activity a) {
		transactionList = new ArrayList<Transaction>();
		transactionList = tList;
		activity = a;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	
	@Override
	public int getCount() {
		return transactionList.size();
	}

	@Override
	public Object getItem(int position) {
		return transactionList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		Transaction transactionItem = transactionList.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.transaction_list_item, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.tViewRequester = (TextView) convertView.findViewById(R.id.tvRequester);
			viewHolder.tViewReceiver = (TextView) convertView.findViewById(R.id.tvReceiver);
			viewHolder.tViewPreviousCredit = (TextView) convertView.findViewById(R.id.tvPreviousCredit);
			viewHolder.tViewAmount = (TextView) convertView.findViewById(R.id.tvTopUpAmount);
			viewHolder.tViewDate = (TextView) convertView.findViewById(R.id.tvTopUpDate);
			viewHolder.tViewTime = (TextView) convertView.findViewById(R.id.tvTime);
			convertView.setTag(viewHolder);
		} 
		viewHolder = (ViewHolder) convertView.getTag();
		viewHolder.tViewRequester.setText(transactionItem.getRequester());
		viewHolder.tViewReceiver.setText(transactionItem.getReceiver());
		viewHolder.tViewPreviousCredit.setText(transactionItem.getPreviousCredit()); 
		viewHolder.tViewAmount.setText(transactionItem.getAmount()); 
		if (transactionItem.getDate() != null && !transactionItem.getDate().contentEquals("null")) {
			viewHolder.tViewDate.setText(transactionItem.getDate().substring(0, 10));
		}
		if (transactionItem.getDate() != null && !transactionItem.getDate().contentEquals("null")) {
			viewHolder.tViewTime.setText(transactionItem.getDate().substring(11, 16));
		}
			
		
		return convertView;
	}
	
	private static class ViewHolder {
		TextView tViewRequester;
		TextView tViewReceiver;
		TextView tViewPreviousCredit;
		TextView tViewAmount;
		TextView tViewDate;
		TextView tViewTime;
	}
}

